 //index.js
//获取应用实例
const app = getApp()

Page({
  data: {
      needShow:false
  },
  //事件处理函数
  onLoad: function () {
    var that = this;
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userInfo']) {
          console.log('111')
          // wx.authorize({
          //   scope: 'scope.userInfo',
          //   success() {
          //     // 用户已经同意小程序使用录音功能，后续调用 wx.startRecord 接口不会弹窗询问
          //     // wx.startRecord()
          //   }
          // })
          that.setData({
            needShow:true
          })
        }
      }
    })

    // wx.request({
    //   url: app.globalData.baseUrl + '/api/serve/coupon',
    //   data: {
    //     session_key: app.gs()
    //   },
    //   success: function (res) {
    //     // console.log(res)
    //     let coupon = res.data.data;
    //     if(coupon == null) {
    //       coupon = []
    //     }
    //     that.setData({
    //       coupon:coupon
    //     })
    //   }
    // })
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    // var session_key = wx.getStorageSync('session_key');
    // console.log('session_key',session_key);
    
    app.wxrequest({
      url: app.globalData.baseUrl + '/api/index/index',
      method: 'GET',
      header: 'application/json',
      data: {
        session_key: app.gs()
      },
      success: res => {
        // console.log('resData', res);
        wx.hideLoading()
        that.setData({
          commodity: res.data.data.commodity,
          banner: res.data.data.banner,
          shop: res.data.data.shop
        })
        wx.setStorageSync('shopname', res.data.data.shop.name);
      }
    })
   
  },
  onShow:function(){
    let that = this;
    // if (!wx.getStorageSync('loginAgain')) {
    //   wx.login({
    //     success: res => {
    //       let code = res.code;
    //       wx.getUserInfo({
    //         success: res => {
    //           console.log('---res', res)
    //           var session = {
    //             url: app.globalData.baseUrl + '/api/user/login',
    //             data: {
    //               code: code,
    //               iv: res.iv,
    //               encryptedData: res.encryptedData
    //             },
    //             method: 'POST',
    //             success: function (res) {
    //               wx.hideLoading();
    //               if (!res.data.data.session_key) {
    //                 app.wxrequest(session)
    //               } else {
    //                 wx.setStorage({
    //                   key: 'session_key',
    //                   data: res.data.data.session_key
    //                 });
    //               }

    //             }
    //           }
    //           app.wxrequest(session)
    //         }
    //       })
    //     }
    //   })
    // }
    app.wxrequest({
      url: app.globalData.baseUrl + '/api/index/index',
      method: 'GET',
      header: 'application/json',
      data: {
        session_key: app.gs()
      },
      success: res => {
        // console.log('resData', res);
        wx.hideLoading()
        that.setData({
          commodity: res.data.data.commodity,
          banner: res.data.data.banner,
          shop: res.data.data.shop
        })
      }
    })
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  reservation(e){
    let _this = this;
    wx.navigateTo({
      url: 'reservation/reservation?shopname='+_this.data.shop.name,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  groupPurchase(e){
    wx.navigateTo({
      url: 'groupPurchase/groupPurchase',
    })
  },
  wineShop(e){
    wx.navigateTo({
      url: 'wineShop/wineShop',
    })
  },
  callService(e){
    wx.navigateTo({
      url: 'callService/callService',
    })
  },
  openMap(e){
    var that = this;
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度
      success: function (res) {
        // var latitude = res.latitude
        // var longitude = res.longitude
        var latitude = parseFloat(that.data.shop.latitude);
        var longitude = parseFloat(that.data.shop.longitude);
        console.log(longitude);
        wx.openLocation({
          latitude: latitude,
          longitude: longitude,
          scale: 28
        })
      }
    })
  },
  callPhone(e){
    wx.makePhoneCall({
      phoneNumber:this.data.shop.photo,
      success(res){
        // console.log(res)
      }
    })
  },
  addWine(e){
    wx.navigateTo({
      url: 'addWine1/addWine1',
    })
  },
  stockRecord(e){
    wx.navigateTo({
      url: "../my/stockRecord/stockRecord",
    })
  },
  myCard(e){
    wx.navigateTo({
      url: '../my/card/card',
    })
  },
  // my(e){
  //   wx.switchTab({
  //     url: '../my/my',
  //   })
  // }
 detail(e){
  //  console.log(e)
   wx.navigateTo({
     url: 'groupPurchase/moreGroupPurchaseDetail/moreGroupPurchaseDetail?com_id='+e.currentTarget.id,
   })
 },
 receiveCoupon(e){
   let _this = this;
   app.wxrequest({
     url: app.globalData.baseUrl +'/api/serve/add_coupon',
     data:{
       session_key:app.gs(),
       cou_id:e.currentTarget.id
     },
     success:function(res){
       wx.hideLoading()
       console.log(res)
       _this.setData({
         coupon:[]
       })
     }
   })
 },
 close(e){
   this.setData({
     coupon:[]
   })
 },
 reload(e) {
   console.log(e)
   if (e.detail.errMsg == 'getUserInfo:ok') {
     let page = getCurrentPages()
     this.setData({
       needShow: false
     })
     page[(page.length - 1) > 0 ? (page.length - 1) : 0].onLoad(page[(page.length - 1) > 0 ? (page.length - 1) : 0].options)
   }
 },
})
